<?php
Prado::using('Application.common.common');
class EditProfile extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " | Update Profile";
		
		if (!$this->IsPostBack)
		{		
			$userRecord = UserRecord::finder()->findByPk($this->Application->User->ID);
			if ($userRecord instanceof UserRecord)
			{
				$this->txtFirstName->Text = $userRecord->FirstName;
				$this->txtLastName->Text = $userRecord->LastName;
				$this->txtAddress->Text = $userRecord->Address;
				$this->txtCity->Text = $userRecord->City;
				$this->txtPostalCode->Text = $userRecord->PostalCode;
				$this->txtEmail->Text = $userRecord->Email;
				$this->txtPhone->Text = $userRecord->Phone;
				$this->txtPhone2->Text = $userRecord->Phone2;
				
				$this->ddlCountry->SelectedValue = $userRecord->Country;	
			}
		}
	}
	
	protected function btnUpdateProfile_Clicked($sender, $param)
	{
		try
		{
			$activeRecord = UserRecord::finder()->findByuser_idAnduser_email($this->Application->User->ID, $this->Application->User->Email);
			if ($activeRecord === null)
			{
				$this->Notice->Type = AdminNoticeType::Error;
				$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","user");
			}
			else
			{
				$activeRecord->Email = $activeRecord->Username = $this->txtEmail->SafeText;
				$activeRecord->FirstName = $this->txtFirstName->SafeText;
				$activeRecord->LastName = $this->txtLastName->SafeText;
				$activeRecord->Phone = $this->txtPhone->SafeText;
				$activeRecord->Phone2 = $this->txtPhone2->SafeText;
				$activeRecord->Address = $this->txtAddress->SafeText;
				$activeRecord->City = $this->txtCity->SafeText;
				$activeRecord->Country = $this->ddlCountry->SelectedValue;
				$activeRecord->PostalCode = $this->txtPostalCode->SafeText;
				$activeRecord->save();
				$this->Response->redirect($this->Service->ConstructUrl("account.Profile", array("m"=>"uu")));
			}
		}
		catch(TException $e)
		{
			$this->Notice->Type = AdminNoticeType::Error;
			$this->Notice->Text = $this->Application->getModule("message")->translate(($activeRecord->ID>0 ? "UPDATE_FAILED" : "ADD_FAILED"),"User",$activeRecord->Email);
		}
	}
	
	protected function btnCancel_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("account.Profile"));
	}
}

?>